package com.eece496.Database;
import java.sql.*;

public class Student extends Database{
	private int studentID;
	private String username = null;
	private String password = null;
	private String firstName = null;
	private String lastName = null;
	private String email = null;
	private String program = null;
	private int year;
	private int GPA;
	
	public Student()
	{
		tableName = "Student";
	}
	
	public Student(int studentID, String username, String password, String firstName, String lastName, String email, String program, int year, int GPA)
	{
		tableName = "Student";
		
		this.studentID = studentID;
		this.username = username;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.program = program;
		this.year = year;
		this.GPA = GPA;
	}
	
	@Override
	public void Insert() throws SQLException {
		// TODO Auto-generated method stub
		Statement stmt = connection.createStatement();
		
	    String sqlString = "INSERT INTO " + tableName + " VALUES (" +
	    					studentID + ", " +			
	    					AddQuotes(username) + ", " +
	    					AddQuotes(password) + ", " +
	    					AddQuotes(firstName) + ", " +
	    					AddQuotes(lastName) + ", " +
	    					AddQuotes(email) + ", " +
	    					AddQuotes(program) + ", " +
	    					year + ", " +
	    					GPA + 
	    					")";
	    stmt.executeUpdate(sqlString);
	    System.out.println(sqlString);
		
	}

	@Override
	public void Delete(String primaryKey) throws SQLException {
		// TODO Auto-generated method stub
		Statement stmt = connection.createStatement();
		String sqlString = "DELETE from " + tableName + " WHERE studentID = " + AddQuotes(primaryKey);
		stmt.executeUpdate(sqlString);
	}

	@Override
	public void Display() throws SQLException {
		// TODO Auto-generated method stub
		
	}
	
	public boolean UserExists(String Username) throws SQLException{
		
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where loginName = " + AddQuotes(Username);
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in databse
		{
			this.studentID = Integer.parseInt(rs.getString("studentID"));
			this.username = rs.getString("loginName");
			this.password = rs.getString("password");
			this.firstName = rs.getString("firstname");
			this.lastName = rs.getString("lastname");
			this.email = rs.getString("email");
			return true;
		}
		else
		{
			return false;
		}	
	}
	
	//Added by steven chow
	public boolean UserExists(int studentID) throws SQLException{
		
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where loginName = " + studentID;
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in databse
		{
			this.studentID = Integer.parseInt(rs.getString("studentID"));
			this.username = rs.getString("loginName");
			this.password = rs.getString("password");
			this.firstName = rs.getString("firstname");
			this.lastName = rs.getString("lastname");
			this.email = rs.getString("email");
			return true;
		}
		else
		{
			return false;
		}	
	}

	public int getStudentID()
	{
		return this.studentID;
	}
	public String getFirstName()
	{
		return this.firstName;
	}
	
	public String getLastName()
	{
		return this.lastName;
	}
	
	public String GetPassword()
	{
		return this.password;
	}
}
